package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.pojo.user.TbUserExample;
import com.dongdongshop.service.user.UserService;
import com.dongdongshop.utils.ShiroUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/3/10 11:20
 */
@Component
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private TbUserMapper userMapper;

    /**
     * 根据据用户查询用户信息
     * @param username
     * @return
     */
    @Override
    public List<TbUser> getUserByUsername(String username) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbUser> tbUsers = userMapper.selectByExample(example);
        return tbUsers;
    }

    /**
     * 注册用户
     * @param user
     */
    @Override
    public void addUser(TbUser user) {
        //给用户密码使用shiro工具类加密
        //第一个参数 加密方式，第二个：用户的明文密码，第三个：用用户的名称当盐，第四个撒几次盐
        String password = ShiroUtils.encryptPassword("MD5", user.getPassword(), user.getUsername(), 1);
        user.setPassword(password);
        //设置注册事件
        user.setCreated(new Date());
        user.setUpdated(new Date());
        userMapper.insertSelective(user);
    }
}
